﻿using System.Collections;
using System.Collections.Generic;

namespace Sustainalytics.ScreeningTool.Synchronization.Caches
{
    public abstract class CacheBase { }

    public abstract class HashSetCache<T> : CacheBase, IEnumerable<T>
    {
        protected readonly HashSet<T> hashSet = new HashSet<T>();

        public void Add(T item) => hashSet.Add(item);

        public void AddItems(IEnumerable<T> items) => hashSet.AddItems(items);

        public bool Contains(T item) => hashSet.Contains(item);

        public IEnumerator GetEnumerator() => hashSet.GetEnumerator();

        IEnumerator<T> IEnumerable<T>.GetEnumerator() => hashSet.GetEnumerator();
    }

    public abstract class DictionaryCache<Tkey, TValue> : CacheBase, IEnumerable
    {
        protected readonly Dictionary<Tkey, TValue> dictionary = new Dictionary<Tkey, TValue>();

        public void Add(Tkey key, TValue value) => dictionary.Add(key, value);

        public void AddItems(Dictionary<Tkey, TValue> items)
        {
            foreach (var item in items)
            {
                dictionary.Add(item.Key, item.Value);
            }
        }

        protected TValue GetValueOrEmpty(Tkey key, TValue emptyValue)
            => dictionary.TryGetValue(key, out var value) ? value : emptyValue;

        public IEnumerator GetEnumerator() => dictionary.GetEnumerator();
    }
}
